package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MiniContext {
    private String name;

    private File webFile;
    private Map<String,MiniWrapper> servletMap = new HashMap<>();
    public MiniContext(){}


    public MiniContext(String name,File webFile){

        this.name = name;
        this.webFile = webFile;

        InputStream resourceAsStream = null;
        try {
            resourceAsStream = new FileInputStream(webFile);

            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(resourceAsStream);

            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element = selectNodes.get(i);
                // <servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletnameElement.getStringValue();

                // <servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletclassElement.getStringValue();

                // 根据servlet-name的值找到url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                // /lagou
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();

                MiniWrapper miniWrapper = new MiniWrapper(servletName,servletClass,urlPattern);

                String classPath = webFile.getParent()
                            +File.separator
                                +servletClass.replaceAll("\\.","/")+".class";
                File f = new File(classPath);

                if(f.exists()){
                    MiniClassLoader miniClassLoader = new MiniClassLoader(f);
                    Class<?> c = miniClassLoader.findClass(servletClass);

                    if(HttpServlet.class.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers())){

                        HttpServlet o = (HttpServlet)c.newInstance();
                        miniWrapper.setHttpServlet(o);

                        servletMap.put(urlPattern,miniWrapper);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public File getWebFile() {
        return webFile;
    }

    public void setWebFile(File webFile) {
        this.webFile = webFile;
    }


    public Map<String, MiniWrapper> getServletMap() {
        return servletMap;
    }

    public void setServletMap(Map<String, MiniWrapper> servletMap) {
        this.servletMap = servletMap;
    }
}
